<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class km_cabang extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('km_cabang_model', 'Cabang');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Cabang";
		viewByTemplate('km_cabang/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->Vendor->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("cabang_kode") == '') {
			$error->Add("cabang_kode", "Kode Cabang", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM km_cabang WHERE kd_vendor = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("cabang_kode"))))."'")) > 0) {
			$error->Add("cabang_kode", "Kode Cabang", $tpl_duplicate);
		}else if (post("cabang_nama") == '') {
			$error->Add("cabang_nama", "Nama Cabang", $tpl_empty);
		}else if (post("cabang_npwp") == '') {
			$error->Add("cabang_npwp", "NPWP Cabang", $tpl_empty);
		}else if (post("cabang_nopkp") == '') {
			$error->Add("cabang_nopkp", "NOPKP Cabang", $tpl_empty);
		}else if (post("cabang_alamat") == '') {
			$error->Add("cabang_alamat", "Alamat Cabang", $tpl_empty);
		}else if (post("cabang_telp") == '') {
			$error->Add("cabang_telp", "Telepon Cabang", $tpl_empty);
		}else if (post("cabang_fax") == '') {
			$error->Add("cabang_fax", "Fax Cabang", $tpl_empty);
		}else if (post("cabang_tipe") == '') {
			$error->Add("cabang_tipe", "Tipe Cabang", $tpl_empty);
		}
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('cabang_kode', 'cabang_nama', 'cabang_npwp', 'cabang_nopkp', 'cabang_alamat', 'cabang_telp','cabang_fax','cabang_tipe'));
				$data['cabang_kode']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['cabang_kode'])));
				
				if (post("mode") == "create")
				{
					$this->Cabang->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->Cabang->Update($data, array('cabang_kode'	=> $data['cabang_kode']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$kd_vendor	= get('cabang_kode');
		
		if ($kd_vendor != "")
		{
			// Delete Single
			$arr_rule	= array('cabang_kode' => $cabang_kode);
			$ls_result = $this->Cabang->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('cabang_kode' => $row['cabang_kode']);
				$ls_result .= $this->Cabang->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>